package org.atlantis.grosbeak.lang.fruit;

import java.lang.reflect.Constructor;

import org.atlantis.grosbeak.lang.Exceptions;

/**
 * 通过构造方法实例化。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 * @param <T>
 */
public class ConstructorFruiting<T> implements Fruiting<T> {

	private Constructor<T> c;

	/**
	 * @param c
	 *            构造方法。
	 */
	public ConstructorFruiting(Constructor<T> c) {
		this.c = c;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.atlantis.grosbeak.lang.fruit.Fruiting#fruit(java.lang.Object[])
	 */
	public T fruit(Object[] args) {
		try {
			if (args == null)
				return c.newInstance();
			return c.newInstance(args);
		} catch (Exception e) {
			throw Exceptions.makeThrow(FruitingException.class,
					"Fail to instantiate with constructor, because: '%s'.", e
							.getMessage());
		}
	}
}
